//
//  UILabel+ContentSize.m
//  u9_iOS
//
//  Created by zhangzhenyun on 2017/8/23.
//  Copyright © 2017年 user1. All rights reserved.
//
#define kDefaultOptions (NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)

#import "UILabel+ContentSize.h"

@implementation UILabel(ContentSize)
- (CGSize)contentSize {
    return [self contentSizeWithOptions:kDefaultOptions];
}
- (CGSize)contentSizeWithOptions:(NSStringDrawingOptions)options {
    return [self contentSizeWithMaxSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:options];
}
- (CGSize)contentSizeWithMaxSize:(CGSize)maxSize {
    return [self contentSizeWithMaxSize:maxSize options:kDefaultOptions];
}
- (CGSize)contentSizeWithMaxSize:(CGSize)maxSize options:(NSStringDrawingOptions)options{
    CGSize size;
    //iOS7之后
    //放置字体属性的字典
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.font,NSFontAttributeName, nil];
    //(NSStringDrawingOptions) 设定换行方式  NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading加起来相当于按照字节换行
    //boundingRectWithSize设定计算范围
    //options 设定三个值相当于按字节换行
    //dic 带有字体属性的字典
    size = [self.text boundingRectWithSize:maxSize options:options attributes:dic context:nil].size;
    size.height = size.height;
    return size;
}


+ (CGSize)contentSizeWithText:(NSString *)text Font:(UIFont *)font {
    return [self contentSizeWithText:text Font:font options:kDefaultOptions];
}
+ (CGSize)contentSizeWithText:(NSString *)text Font:(UIFont *)font options:(NSStringDrawingOptions)options{
    CGSize size;
    //iOS7之后
    //放置字体属性的字典
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil];
    //(NSStringDrawingOptions) 设定换行方式  NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading加起来相当于按照字节换行
    //boundingRectWithSize设定计算范围
    //options 设定三个值相当于按字节换行
    //dic 带有字体属性的字典
    size = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 999) options:options attributes:dic context:nil].size;
    size.height = size.height + 1;
    return size;
}

@end


